package lainey.ui.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.text.Editable;
import android.text.InputFilter;
import android.text.Selection;
import android.text.Spannable;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.KeyListener;
import android.text.method.PasswordTransformationMethod;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import lainey.ui.R;

public class EditTextDialog extends Dialog {

    private TextView mTvTitle;
    private TextView mTvLeft;
    private TextView mTvRight;
    private ImageView mIvEye;
    private ImageView mIvClear;
    private EditText mEditText;

    private boolean mIsShowPassword = true;
    private boolean mIsPasswordMode = false;

    public EditTextDialog(@NonNull Context context) {
        super(context, R.style.UI_DefaultDialogStyle);
        initDialog(context);
    }

    private void initDialog(Context context) {
        setContentView(R.layout.ui_dialog_edit_text);
        Window window = getWindow();
        if (window != null) {
            window.getAttributes().gravity = Gravity.CENTER;
        }
        setCancelable(false);
        setCanceledOnTouchOutside(true);
        if (context instanceof Activity) {
            Activity activity = (Activity) context;
            setOwnerActivity(activity);
        }

        mEditText = findViewById(R.id.edit_text);
        mIvEye = findViewById(R.id.iv_eye);
        mIvClear = findViewById(R.id.iv_clear);
        mEditText.addTextChangedListener(textWatcher);
        mIvEye.setOnClickListener(
                (v) -> showPassword()
        );
        mIvClear.setOnClickListener(
                v -> mEditText.setText("")
        );
        mTvTitle = findViewById(R.id.tv_title);
        mTvLeft = findViewById(R.id.tv_left);
        mTvRight = findViewById(R.id.tv_right);
        mIvEye.setVisibility(View.GONE);
        mIvClear.setVisibility(View.INVISIBLE);
    }

    private void showPassword() {
        if (mIsShowPassword) {
            HideReturnsTransformationMethod instance = HideReturnsTransformationMethod.getInstance();
            mEditText.setTransformationMethod(instance);
            mIvEye.setImageResource(R.drawable.ui_ic_eye_press);
            mIsShowPassword = false;
        } else {
            PasswordTransformationMethod instance = PasswordTransformationMethod.getInstance();
            mEditText.setTransformationMethod(instance);
            mIvEye.setImageResource(R.drawable.ui_ic_eye);
            mIsShowPassword = true;
        }
        Spannable spanText = mEditText.getText();
        int length = spanText.length();
        Selection.setSelection(spanText, length);
    }

    public void setTextMode() {
        mIsShowPassword = true;
        mIsPasswordMode = false;
        HideReturnsTransformationMethod instance = HideReturnsTransformationMethod.getInstance();
        mEditText.setTransformationMethod(instance);
        mEditText.addTextChangedListener(textWatcher);
        mIvEye.setImageResource(R.drawable.ui_ic_eye);
        mIvEye.setVisibility(View.GONE);
        mIvClear.setVisibility(View.INVISIBLE);
    }

    public void setPasswordMode() {
        mIsShowPassword = true;
        mIsPasswordMode = true;
        PasswordTransformationMethod instance = PasswordTransformationMethod.getInstance();
        mEditText.setTransformationMethod(instance);
        mEditText.removeTextChangedListener(textWatcher);
        mIvEye.setImageResource(R.drawable.ui_ic_eye);
        mIvClear.setVisibility(View.INVISIBLE);
        mIvEye.setVisibility(View.VISIBLE);
    }

    public String getText() {
        return mEditText.getText().toString();
    }

    public EditText getEditText() {
        return mEditText;
    }

    public void setLeftOnClickListener(View.OnClickListener listener) {
        mTvLeft.setOnClickListener(listener);
    }

    public void setRightOnClickListener(View.OnClickListener listener) {
        mTvRight.setOnClickListener(listener);
    }

    public void setTitle(String text) {
        mTvTitle.setText(text);
    }

    public void setTitleText(int resId) {
        mTvTitle.setText(resId);
    }

    public void setLeftText(String text) {
        mTvLeft.setText(text);
    }

    public void setLeftText(int redId) {
        mTvLeft.setText(redId);
    }

    public void setRightText(String text) {
        mTvRight.setText(text);
    }

    public void setRightText(int resId) {
        mTvRight.setText(resId);
    }

    /**
     * 设置输入类型
     */
    public void setInputType(int type) {
        mEditText.setInputType(type);
    }

    /**
     * 设置输入框的文字
     */
    public void setText(String content) {
        if (content != null && content.length() > 0) {
            int length = content.length();
            mEditText.setText(content);
            mEditText.setSelection(length);
        } else {
            mEditText.setText("");
        }
    }

    /**
     * 设置输入框的最大长度
     */
    public void setMaxLength(int num) {
        InputFilter.LengthFilter filter = new InputFilter.LengthFilter(num);
        InputFilter[] filters = {filter};
        mEditText.setFilters(filters);
    }

    /**
     * 设置输入框提示文字
     */
    public void setHintText(String text) {
        mEditText.setHint(text);
    }

    public void setKeyListen(KeyListener listener) {
        mEditText.setKeyListener(listener);
    }

    public void showSoftInput() {
        InputMethodManager inputManager = (InputMethodManager) mEditText.getContext()
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        if (inputManager != null) {
            inputManager.showSoftInput(mEditText, 0);
        }
    }

    public void showKeyboard() {
        // 设置可获得焦点
        mEditText.setFocusable(true);
        mEditText.setFocusableInTouchMode(true);
        // 请求获得焦点
        mEditText.requestFocus();
        // 调用系统输入法
        InputMethodManager inputManager = (InputMethodManager) mEditText.getContext()
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        if (inputManager != null) {
            inputManager.showSoftInput(mEditText, 0);
        }
    }

    private TextWatcher textWatcher = new TextWatcher() {

        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int start, int count, int after) {

        }

        @Override
        public void afterTextChanged(Editable editable) {
            if (mIsPasswordMode) return;
            if (editable.length() > 0) {
                mIvClear.setVisibility(View.VISIBLE);
            } else {
                mIvClear.setVisibility(View.INVISIBLE);
            }
        }

    };


}
